package com.eschool.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.converter.SectionConverter;
import com.eschool.model.Section;
import com.eschool.model.Student;
import com.eschool.service.StudentService;
import com.eschool.service.impl.StudentServiceImpl;

@ManagedBean(name="studentBean")
@ViewScoped
public class StudentBean {
	private Student student = new Student();
	StudentService service = new StudentServiceImpl();	
	List<Section> sections;
	
	public StudentBean() {
		sections = new ArrayList<Section>();
		sections = SectionConverter.sectionDB;
	}
	
	public List<Section> getSections() {
		return sections;
	}

	public void setSections(List<Section> sections) {
		this.sections = sections;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public String perform()
	{
		//student.setSection(SectionDao.getSectionByProperty("sectionId", 2));
		//student.setDob(new Date());
		
		boolean status = service.addStudent(student);

		System.out.print(status);
		
		if(status)
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO,"Info message", "Student Succesfully Added"));
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR,"Error message", "Student Not Added"));
		return "AddStudent";
	}
}
